-- pathfinding
-- from https://love2d.org/wiki/Tutorial:Gridlocked_Player


function love.load()

	math.randomseed(os.time())
	
	interval = 2
	tick = 0

    player1 = {
        grid_x = 128,
        grid_y = 224,
        act_x = 128,
        act_y = 224,
		rand_number = 0,
        speed = 10,
		lastpress = 0,
		text = ""
    }
	
	
	
    map = {
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
    }
end

function love.update(dt)
    player1.act_y = player1.act_y - ((player1.act_y - player1.grid_y) * player1.speed * dt)
    player1.act_x = player1.act_x - ((player1.act_x - player1.grid_x) * player1.speed * dt)
	
	tick = tick + dt
	if tick > interval then
		--Here you call the function ,generate random number and all that stuff
		--wander()
		tick = 0
	end
end

function love.draw()
	love.graphics.print("Our random number is (" .. player1.rand_number .. ")", 500, 500)
	
	if player1.text ~= "" then
		love.graphics.print(player1.text, 500, 550)
	end
	
    love.graphics.rectangle("fill", player1.act_x, player1.act_y, 32, 32)
    for y=1, #map do
        for x=1, #map[y] do
            if map[y][x] == 1 then
                love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
            end
        end
    end
end

function love.keypressed(key)
	player1.lastpress = 0
    if key == "up" then
        if testMap(0, -1) then
            player1.grid_y = player1.grid_y - 32
        end
    elseif key == "down" then
        if testMap(0, 1) then
            player1.grid_y = player1.grid_y + 32
        end
    elseif key == "left" then
        if testMap(-1, 0) then
            player1.grid_x = player1.grid_x - 32
        end
    elseif key == "right" then
        if testMap(1, 0) then
            player1.grid_x = player1.grid_x + 32
        end
    end
end

function testMap(x, y)
    if map[(player1.grid_y / 32) + y][(player1.grid_x / 32) + x] == 1 then
        return false
    end
    return true
end

function wander()

player1.rand_number = math.random(100)

if player1.rand_number < 99 then
	-- do something
	rand2 = math.random(4)
	
	if rand2 == 1 then
		if testMap(0, -1) then
            player1.grid_y = player1.grid_y - 32
        end
	elseif rand2 == 2 then
		if testMap(0, 1) then
            player1.grid_y = player1.grid_y + 32
        end
	elseif rand2 == 3 then
		if testMap(-1, 0) then
            player1.grid_x = player1.grid_x - 32
        end
	elseif rand2 == 4 then
		if testMap(1, 0) then
            player1.grid_x = player1.grid_x + 32
        end
	end
	
	if player1.rand_number < 20 then
		player1.text = "Man, I am SO lost!"
	else player1.text = ""
	end
	
end


end